package com.zhiwang.planet.widget

import android.app.AlertDialog
import android.content.Context
import android.view.Gravity
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import com.zhiwang.planet.R
import kotlinx.android.synthetic.main.simple_dialog_layout.view.*

class SimpleDialogBuilder {


    var scaleAnimate: Boolean = false
    var buttonText: String = ""
    var content: String = ""
    var contentTextSize = 14f
    var buttonClickListener: View.OnClickListener = object : View.OnClickListener {
        override fun onClick(v: View?) {
            //no op
        }
    }
    @ColorRes
    var contentTextColor: Int = R.color.appTextColorLight
    var title: String? = ""//如果标题为空那么隐藏标题栏和分割线
    var titleTextSize = 22f//sp
    var centerText: Boolean = true
    var titleTextColor: Int = R.color.colorPrimary


    var buttonTextSize = 18f//sp

    fun buttonText(buttonText: String): SimpleDialogBuilder {
        this.buttonText = buttonText
        return this
    }

    fun buttonTextSizeSp(textSize: Float): SimpleDialogBuilder {
        this.buttonTextSize = textSize
        return this
    }

    private var hasSetButtonClick: Boolean = false

    fun onButtonClick(listener: View.OnClickListener): SimpleDialogBuilder {
        this.hasSetButtonClick = true
        this.buttonClickListener = listener
        return this
    }

    fun content(content: String): SimpleDialogBuilder {
        this.content = content
        return this
    }

    fun contenxtTextSize(contentTextSize: Float): SimpleDialogBuilder {
        this.contentTextSize = contentTextSize
        return this
    }

    fun contentTextColor(contentTextColor: Int): SimpleDialogBuilder {
        this.contentTextColor = contentTextColor
        return this
    }

    fun title(title: String): SimpleDialogBuilder {
        this.title = title
        return this
    }

    fun notCenter(): SimpleDialogBuilder {
        this.centerText = false
        return this
    }

    fun scaleAnim(): SimpleDialogBuilder {
        this.scaleAnimate = true
        return this
    }

    fun titleTextSizeSp(textSize: Float): SimpleDialogBuilder {
        this.titleTextSize = textSize
        return this
    }

    fun titleTextColor(textColor: Int): SimpleDialogBuilder {
        this.titleTextColor = textColor
        return this
    }
    //input dialog

    fun build(context: Context): AlertDialog {

        val layoutInflater = LayoutInflater.from(context)
        val dialogView = layoutInflater.inflate(R.layout.simple_dialog_layout, null)
        var alertDialog = AlertDialog.Builder(context)
                .setView(dialogView)
                .create()
        dialogView.dialog_button.setOnClickListener {
            if (hasSetButtonClick) {
                buttonClickListener.onClick(it)
            }
            alertDialog.dismiss()
        }

        if (scaleAnimate){
            alertDialog?.window?.getAttributes()?.windowAnimations =R.style.ScalePopTheme
        }
        dialogView.dialog_content.gravity = if (centerText) Gravity.CENTER else Gravity.LEFT
        dialogView.dialog_content.text = content
        dialogView.dialog_content.textSize = contentTextSize
        dialogView.dialog_content.setTextColor(ContextCompat.getColor(context, contentTextColor))
        dialogView.dialog_title.text = title
        dialogView.dialog_title.textSize = titleTextSize
        dialogView.dialog_title.setTextColor(ContextCompat.getColor(context, titleTextColor))
        dialogView.dialog_button.text = buttonText
        dialogView.dialog_button.textSize = buttonTextSize

        return alertDialog

    }

}